/**
 * Created by IntelliJ IDEA.
 * User: Alexey
 * Date: 1/26/11
 * Time: 10:18 PM
 * To change this template use File | Settings | File Templates.
 */
package model.vo{
    import org.kolonitsky.alexey.utils.DateUtil;

    [Bindable]

    /**
     * Record is simple operation in user balance log
     */
    public dynamic class LogRecord
    {
        //-------------------------------------------------------------------------
        //
        //  Properties
        //
        //-------------------------------------------------------------------------

        public var author:String = "";
        public var comment:String = "";
        public var date:Date = null;
        public var id:String = "";
        public var value:Object = {};
        public var wallets:Array = [];
        public var enabled:Boolean = true;

        /**
         * Calculated as sum of all currencies value.
         */
        public function get amount():Number
        {
            var result:Number = 0.0;
            for (var currency:String in value)
                result += Number(value[currency]);
            return result;
        }

        //-------------------------------------------------------------------------
        //
        //  Methods
        //
        //-------------------------------------------------------------------------

        /**
         * @constructor
         * @param init is JSON decoded object received from server
         */
        public function LogRecord(init:* = null)
        {
            if (init is Date)
            {
                date = init as Date
            }
            else if (init is String)
            {
                comment = String(init);
            }
            else if (init)
            {
                author = init.author;
                comment = init.comment;
                date = DateUtil.createDate(init.date);
                id = init.id;
                for (var wallet:String in init)
                {
                    if (wallet.charAt(0) == "@")
                    {
                        wallets.push(wallet);
                        value = init[wallet];
                        for (var p:String in value)
                            this[p] = value[p];
                    }
                }
            }
            else
            {
                date = new Date();
            }
        }

        public function clone():LogRecord
        {
            var result:LogRecord = new LogRecord();
            result.author = author;
            result.comment = comment;
            result.date = new Date(date.time);
            result.id = id;
            result.value = {};
            result.wallets = wallets.slice();
            for (var currency:String in value)
            {
                result.value[currency] = Number(value[currency]);
                result[currency] = Number(value[currency]);
            }
            return result;
        }

        /**
         * Convert LogRecord to Object
         * @return plain object for encode to JSON or save as AMF
         */
        public function toObject():Object
        {
            var result:Object = {};
            result.author = author;
            result.comment = comment;
            result.id = id;
            result.date = DateUtil.dateToUnix(date);
            var v:Object = {};
            for (var currency:String in value)
                v[currency] = Number(value[currency]);
            result[wallets[0]] = v;
            return result;
        }

        //-------------------------------------------------------------------------
        // Value logics
        //-------------------------------------------------------------------------

        /**
         * Add current value to value passed as function argument
         * @param value
         */
        public function addValue(value:Object):void
        {
            for (var currency:String in value)
            {
                if (currency in this.value)
                    this.value[currency] += Number(value[currency]);
                else
                    this.value[currency] = Number(value[currency]);
                this[currency] = this.value[currency];
            }
        }

        /**
         * Substact current value with value passed as function argument
         * @param value for substraction
         */
        public function subValue(value:Object):void
        {
            for (var currency:String in value)
            {
                if (currency in this.value)
                    this.value[currency] -= Number(value[currency]);
                else
                    this.value[currency] = Number(-value[currency]);
            }
        }

        /**
         * count currencies
         * @return
         */
        public function numValue():int
        {
            var result:int = 0;
            for (var currency:String in value)
                result++;
            return result;
        }

        /**
         *
         * @return formatted string with all values of record
         */
        public function valueToString():String
        {
            var str:String = "";
            for (var currency:String in value)
                str += value[currency] + " " + currency + " ";
            return str;
        }

        /**
         *
         * @return
         */
        public function valueToMultilineString():String
        {
            var str:String = "";
            for (var currency:String in value)
                str += value[currency] + " " + currency + "\n";
            return str.substr(0, str.length - 1);
        }


        //-------------------------------------------------------------------------
        // Tags logic
        //-------------------------------------------------------------------------

        private var _tags:Array = null;

        /**
         * Fetch a hash tags from the current record
         * @return Array of strings used in comment field
         */
        public function get tags():Array
        {
            if (_tags)
                return _tags;

            var tagPattern:RegExp = /#(\S+)(\s|$)/img;
            var result:Array = comment.match(tagPattern);
            for (var i:int = 0; i < result.length; i++)
            {
                var str:String = result[i];
                var endIndex:int = str.length;
                if (str.charAt(str.length - 1) == " ")
                    endIndex = str.length - 1;
                result[i] = str.substring(1, endIndex);
            }
            _tags = result || []
            return _tags;
        }
    }
}
